package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class JYSearchVisitActivity extends AppCompatActivity {
    private TimePickerView mpv1;
    private TimePickerView mpv2;
    private TimePickerView mpv3;
    private TimePickerView mpv4;
    String stime;
    String etime;
    String stime1;
    String etime1;
    Date date1,date2,date3,date4;
    TextView stimetv,etimetv,stimetv1,etimetv1;
    private LinearLayout linearLayout;
    private ImageView img_lr,img_cx,img_tj,img_me,img_back;
    int id;
    private ImageView img_qk,img_qk1,img_qk2,img_qk3;
    private Button btn_commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jysearch_visit);
        btn_commit = (Button) findViewById(R.id.btn_commit);
        img_qk= (ImageView) findViewById(R.id.qingkong);
        img_qk1= (ImageView) findViewById(R.id.qingkong1);
        img_qk2= (ImageView) findViewById(R.id.qingkong2);
        img_qk3= (ImageView) findViewById(R.id.qingkong3);
        stimetv = (TextView) findViewById(R.id.start_time);
        etimetv = (TextView) findViewById(R.id.end_time);
        stimetv1 = (TextView) findViewById(R.id.start_time1);
        img_cx= (ImageView) findViewById(R.id.item_image_cx);
        img_lr= (ImageView) findViewById(R.id.item_image_lr);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        etimetv1 = (TextView) findViewById(R.id.end_time1);
        linearLayout= (LinearLayout) findViewById(R.id.search_linearLayout);
        id = getIntent().getIntExtra("id",0);
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 = date;
                stime=getTime(date);
                stimetv.setText(stime);
            }
        });
        img_qk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stimetv.setText("");
            }
        });
        mpv2 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv2.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv2.setTime(new Date());
        //选择监听
        mpv2.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                etime=getTime(date);
                etimetv.setText(etime);
            }
        });
        img_qk1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etimetv.setText("");
            }
        });
        //创建选择器
        mpv3 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar3 = Calendar.getInstance();
        mpv3.setRange(calendar.get(Calendar.YEAR) - 100, calendar3.get(Calendar.YEAR) + 100);
        //插入时间
        mpv3.setTime(new Date());
        //选择监听
        mpv3.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date3 = date;
                stime1=getTime(date);
                stimetv1.setText(stime1);
            }
        });
        img_qk2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stimetv1.setText("");
            }
        });
        mpv4 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar4 = Calendar.getInstance();
        mpv4.setRange(calendar1.get(Calendar.YEAR) - 100, calendar4.get(Calendar.YEAR) + 100);
        //插入时间
        mpv4.setTime(new Date());
        //选择监听
        mpv4.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date4 = date;
                etime1=getTime(date);
                etimetv1.setText(etime1);
            }
        });
        img_qk3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etimetv1.setText("");
            }
        });
            img_tj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("tt","tt");
                    startActivity(intent);
                    finish();

                }
            });
            img_me.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("me","me");
                    startActivity(intent);
                    finish();
                }
            });
            img_lr.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchVisitActivity.this, JDcoorActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
            img_cx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(JYSearchVisitActivity.this, JDcoorActivity.class);
                    intent.putExtra("ii","ii");
                    startActivity(intent);
                    finish();
                }
            });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (date1!=null&&date2!=null&&date1.after(date2)){
                    Toast.makeText(JYSearchVisitActivity.this, "开始时间不能在结束时间之后", Toast.LENGTH_SHORT).show();
                }else if(date3!=null&&date4!=null&&date3.after(date4)){
                    Toast.makeText(JYSearchVisitActivity.this, "开始时间不能在结束时间之后", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(JYSearchVisitActivity.this, JYCoordVisitActivity.class);
                    intent.putExtra("tag2", "k2");
                    intent.putExtra("KXA01", id);
                    intent.putExtra("KPA0601", stime);
                    intent.putExtra("KPA0602", etime);
                    intent.putExtra("CTS0101", stime1);
                    intent.putExtra("CTS0102", etime1);
                    startActivity(intent);
                    finish();

                }



            }
        });
    }
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start_show:
                mpv1.show();
                break;
            case R.id.end_show:
                mpv2.show();
                break;
            case R.id.start_show1:
                mpv3.show();
                break;
            case R.id.end_show1:
                mpv4.show();
                break;

        }
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
